﻿using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.eShopWeb.FunctionalTests.PublicApi;
using Microsoft.eShopWeb.Web.ViewModels;
using Xunit;

namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;

[Collection("Sequential")]
public class ApiCatalogControllerList : IClassFixture<ApiTestFixture>
{
    public ApiCatalogControllerList(ApiTestFixture factory)
    {
        Client = factory.CreateClient();
    }

    public HttpClient Client { get; }

    [Fact]
    public async Task ReturnsFirst10CatalogItems()
    {
        var response = await Client.GetAsync("/api/catalog-items?pageSize=10");
        response.EnsureSuccessStatusCode();
        var stringResponse = await response.Content.ReadAsStringAsync();
        var model = stringResponse.FromJson<CatalogIndexViewModel>();

        Assert.Equal(10, model.CatalogItems.Count());
    }

    [Fact]
    public async Task ReturnsLast2CatalogItemsGivenPageIndex1()
    {
        var response = await Client.GetAsync("/api/catalog-items?pageSize=10&pageIndex=1");
        response.EnsureSuccessStatusCode();
        var stringResponse = await response.Content.ReadAsStringAsync();
        var model = stringResponse.FromJson<CatalogIndexViewModel>();

        Assert.Equal(2, model.CatalogItems.Count());
    }
}
